<?php
class Controller_Admin_ScenicSpot extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type = 'scenicspot';
        $this->_list_pagesize=15;
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=', 'VALID'),
        );
    }

    public function action_seticon ($nid)
    {
        $post = $this->request->post();
        if (empty($post))
        {
            $dirname = '/resource/www/imgs/icon';
            $files = scandir(DOCROOT.'resource/www/imgs/icon'); 
            $icons = array();
            foreach ($files as $f)
            {
                if (preg_match('/\.png$/', $f))
                    $icons[] = "$dirname/$f";
            }

            $scenic = new ScenicSpot;
            $this->template->set('icons', $icons);
            $this->template->set('scenic', $scenic->detail($nid));
            $this->template->set_filename('smarty:www/scenicspot/admin_icon');
        }
        else
        {
            $iconUrl = Arr::get($post, 'iconUrl');
            if (! empty($iconUrl))
            {
                $scenic = new ScenicSpot($nid);
                $scenic->setIconUrl($iconUrl);
            }

            $this->to_url('list');
        }
    }

    public function action_index ()
    {
        $this->action_list();
    }
    
    public function action_list($page = 1){
    	$filter = array();
    	$query  = $this->request->query();
    
    
    	$filter['sceName'] = Arr::get($query, 'scename');

        $filter['city'] = Arr::get($query, 'city');
        $filter['city_options']=App_City::getCityListCnWithId();

        $filter['type']  = Arr::get($query, 'type');
        $filter['type_options']=App_Scenictype::getTypeListWithCityCn();

    	if(!empty($filter['sceName'])){
    		$this->_list_where[] = array('name', 'like', '%'.$filter['sceName'].'%');
    	}
    
    	if(!empty($filter['city'])){
    		$this->_list_where[] = array('cityId', '=', $filter['city']);
    	}
    
    	if(!empty($filter['type'])){
    		$this->_list_where[] = array('type', '=', $filter['type']);
    	}
        
    	$this->template->set('form_include_head_file', $this->_form->include_head_file());
    	$this->template->set('filter', $filter);
    
    	parent::action_list($page);
    }

    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

    public function action_update ($nid=null)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid);
            if(isset($data['images']))
            $data['images'] = explode('|',$data['images']);
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

    public function action_delete ($nid)
    {
        $scenic = new ScenicSpot($nid);
        $scenic->remove($nid);
        $this->to_url('list');
    }

    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->_form_data = $data;
        parent::action_detail($nid);
    }

    private function rebuild_post_data (& $post)
    {
    	//此处uploadify已在前端处理，因此$post的值为url
        if(isset($post['thumb'])){
        foreach($post['thumb'] as $v){
            $file_name = DOCROOT.'/'.$v;
            $image = Image::factory($file_name);
            file_put_contents($file_name,$image->resize('350', '230', Image::NONE)->render());
        }
        }

        if(isset($post['cover'])){
        foreach($post['cover'] as $v){
            $file_name = DOCROOT.'/'.$v;
            $image = Image::factory($file_name);
            file_put_contents($file_name,$image->resize('1380', NULL, Image::INVERSE)->render());
        }
        }

        if(isset($post['images'])){
        foreach($post['images'] as $v){
            $file_name = DOCROOT.'/'.$v;
            $image = Image::factory($file_name);
            file_put_contents($file_name,$image->resize('450', '300', Image::NONE)->render());
        }
        $post['images'] = implode('|', $post['images']);
        }
        
        //系统创建的景点(来自于用户自定义或是来自于系统，type!=2) managerUid>0
        //自定义景点(type=2) managerUid=0
        if($post['type']!=2){
            $post['managerUid'] = user::current_user_id();
            if(empty($post['guiderUid'])){
            	$post['guiderUid']=0;
            }
        }else{
        	$post['managerUid'] = 0;
        }
        
        $city=App_City::getNameCnById($post['cityId'], 'cityName');
        $country=App_City::getNameCnById($post['cityId'], 'countryName');
        $post['city']=$city;
        $post['country']=$country;
    }

    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
          $ret = App_Scenictype::getTypeNameById($rs['type']);
          $rs['type']=$ret;
        }
    }


    public function classname_by_list_row (& $row)
    {
    }

    public function actions_by_list_row (& $row, & $menus)
    {
	    $controller = Request::initial()->controller();
	    $directory  = Request::initial()->directory();
	
	    $directory  = empty($directory) ? '' : '/'.$directory;

        $menus[0] = '<a href="/scenicspot/detail/'.$row['id'].'">查看</a>';
        $menus[] = '<a href="/admin/scenicspot/seticon/'.$row['id'].'">设置图标</a>';
        $menus[] = '<a href="/admin/scenicspot/comment/'.$row['id'].'">评论</a>';
    }

    public function action_comment($id){
        $action = 'scenicspot';
        $tid = $id;
        $obj = new Comments();
        $data = $obj->getComments($action,$tid);
        $this->template->set('data',$data);
        $this->template->set_filename('smarty:www/scenicspot/admin_comment');
    }

    public function action_remove($id){
        $referrer = $this->request->referrer();
        $obj = new Comments();
        $obj->remove_comment($id);
        $this->request->redirect($referrer);
    }

    public function action_optionsCityListByCountry ($countryId)
    {
        $key1='homepage_select_country';
        $country = DB::select('string')
        ->from('mod_config_array')
        ->where('key', '=', $key1)
        ->where('id','=',$countryId)
        ->limit(1)
        ->execute()
        ->get('string');

    	$key2='homepage_select_city';
    	$query = DB::select('id','string')
    	->from('mod_config_array')
    	->where('key', '=', $key2)
    	->where('name','=',$country)
    	->execute();
    	
    	$cityList=array();
    	foreach ($query as $v)
    	{$cityList[$v['id']]=$v['string'];}
    
    	return json_encode($cityList);
    }
}


